package lipengfei.botaiframework.validate.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


import cn.hutool.core.util.StrUtil;
import lipengfei.botaiframework.validate.annotation.IDCard;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 身份证照号码校验
 */
public class IDCardValidator implements ConstraintValidator<IDCard,String> {

    private static final Pattern PATTERN = Pattern.compile("(^[1-9]\\d{5}(19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$)");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 不为null才进行校验
        if (StrUtil.isNotBlank(value)) {
            Matcher matcher = PATTERN.matcher(value);
            return matcher.find();
        }
        return true;
    }
}
